package com.heima.kafka.listener;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class HelloListener {
    /*参数：生产者发送的消息*/
    @KafkaListener(topics = {"kafka-hello"})
    public void receiveMessage(ConsumerRecord<?, ?> record) {
        /*ofNullable方法：如果为非null，返回描述指定值的 Optional，否则返回空的Optional。*/
        Optional<? extends ConsumerRecord<?, ?>> recordResponse = Optional.ofNullable(record);
        /*isPresent()：如果存在值，则返回true，否则返回false*/
        if (recordResponse.isPresent()) {
            record = recordResponse.get();
            System.out.println(record.key());
            System.out.println(record.value());
        }

    }

}
